import type { Request, Response } from 'express';
import {
  responseSuccess,
  responseError,
  responseData,
} from '@/share/interface/standard';
import Base from '@/controller/public/MiniBaseController';
import { uploadHandle } from '@/middleware/multer';
import { IModule } from '@/share/interface/module';

export class DrugReminderController extends Base {
  async getDrugRemindForUser(req: Request) {
    const user_id = req.user_id;
    if (!user_id)
      return responseData({
        code: 201,
        msg: '用户不存在',
        data: null,
      });
    const drug_reminder = await this.drugReminderRepository.find({
      where: { user_id },
    });
    // await req.addModuleClick({
    //   user_id,
    //   module_name: IModule.Drug,
    // });
    return responseSuccess(drug_reminder);
  }
  async addDrugRemindForUser(req: Request) {
    const {
      title,
      open,
      remind_time,
      description,
      user_id = req.user_id,
    } = req.body;

    const user = await this.userRepository.findOne({
      where: { id: user_id },
    });
    if (!user)
      return responseData({
        code: 201,
        msg: '用户不存在',
        data: null,
      });

    await this.drugReminderRepository.save({
      title,
      open,
      remind_time,
      description,
      user_id,
    });

    return responseSuccess();
  }
}
export class DrugKnowledgeController extends Base {
  async findAll() {
    const drug_knowledge = await this.drugKnowledgeRepository.find();
    return responseSuccess(drug_knowledge);
  }

  async addDrugKnowledge(req: Request, res: Response) {
    const final_file = await uploadHandle(req, res);
    console.log(final_file);
    const { title, description, content, id = null } = req.body;

    await this.drugKnowledgeRepository.save({
      id: +id,
      title,
      content,
      description,
      url: final_file.final_host + final_file.filename,
    });
    return responseSuccess();
  }
}
